function getCookie(name) {
  var value = "; " + document.cookie;
  var parts = value.split("; " + name + "=");
  if (parts.length == 2) return parts.pop().split(";").shift();
}
$(document).ready(function(){
   $('.comments').on('click','.remove_sc_cmt',function(){
      var post_id  = $(this).attr('data-pid');
      var type  = $(this).attr('data-type');
      var data = {name:'remove_sc_cmt'};
      loadAjax(data,'json',{
         success:function(res) {
            if(res.status == 1) {
               $('.add_comment_meta').html('<input type="text" name="add_comment_email" class="add_comment_email" placeholder="Nhập email của bạn (*)" /><input type="text" name="add_comment_name" class="add_comment_name" placeholder="Nhập tên của bạn (*)" /><button class="add_comment_btn" data-pid="'+post_id+'" data-type="'+type+'">Gửi</button>');
               $('.imgloading').remove();
            }
         },
         beforeSend:function() {
            $('.add_comment_meta').append('<img style="display: inherit; margin: 5px auto;" class="imgloading" src="/themes/img/loadingcmt.GIF" />');
         } 
      });
   });
   $('.comments').on('click','.add_comment_btn',function(){
      var post_id = $(this).attr('data-pid');
      var type = $(this).attr('data-type');
      var com_email = $('.add_comment_email').val();
      var com_name = $('.add_comment_name').val();
      var com_content = $('.add_comment_content').val();
      if(com_content == '') {
         alert('Vui lòng nhập nội dung bình luận !');
      }else if(com_email == '') {
         alert('Vui lòng nhập email của bạn !');
      }else if(!isemail(com_email)) {
         alert('Vui lòng nhập 1 email hợp lệ !');
      }else if(com_name == '') {
         alert('Vui lòng nhập tên của bạn !');
      }else {
         var data = {name:'addcomment',com_post_id:post_id,com_type:type,com_email:com_email,com_name:com_name,com_content:com_content};
         loadAjax(data,'json',{
            success:function(res) {
               if(res.status == 1) {
                  $('.list_comment_count').after(res.html);
                  $('.imgloading').remove();
                  $('.add_comment_email').val('');
                  $('.add_comment_name').val('');
                  $('.add_comment_content').val('');
                  $('.add_comment_meta').html('<span>Bình luận với tên <strong title="'+com_email+'">'+com_name+'</strong> (<a class="remove_sc_cmt" href="javascript:"  data-pid="'+post_id+'" data-type="'+type+'">thoát</a>) </span><input type="hidden" name="add_comment_email" class="add_comment_email" value="'+com_email+'" /><input type="hidden" name="add_comment_name" class="add_comment_name" value="'+com_name+'" /><button class="add_comment_btn" data-pid="'+post_id+'" data-type="'+type+'">Gửi</button>');
               }else {
                  alert(res.html);
                  $('.imgloading').remove();
                  $('.add_comment_email').val('');
                  $('.add_comment_name').val('');
                  $('.add_comment_content').val('');                  
               }
            },
            beforeSend:function() {
               $('.add_comment_meta').append('<img style="display: inherit; margin: 5px auto;" class="imgloading" src="/themes/img/loadingcmt.GIF" />');
            } 
         });
      }
   });
   $('.comments').on('click','.cmt_reply',function(){
      var com_name = getCookie('comment_name');
      var com_email = getCookie('comment_email');
      if(typeof(com_email) != 'undefined') {
         com_email = com_email.replace("%40", "@");
      }
      var cmt_id = $(this).attr('data-id');
      $('.reply_box').remove();
      var html_rep = '';
      html_rep += '<div class="reply_box">';
      html_rep += '<div class="reply_comment_avatar">';
      html_rep += '<img src="/themes/img/avatar_guest.jpg" alt="avatar guest reply new comments" />';
      html_rep += '</div>';
      html_rep += '<div class="reply_comment_main">';
      html_rep += '<textarea class="reply_comment_content"></textarea>';
      html_rep += '<div class="reply_comment_meta">';
      if(typeof(com_name) != 'undefined') {
         var com_name_utf8 = $('.add_comment_meta').find('strong').html();
         html_rep += '<span>Trả lời với tên <strong title="'+com_email+'">'+com_name_utf8+'</strong></span>';
         html_rep += '<input type="hidden" name="reply_comment_email" class="reply_comment_email" value="'+com_email+'" />';                 
         html_rep += '<input type="hidden" name="reply_comment_name" class="reply_comment_name" value="'+com_name_utf8+'" />';              
         html_rep += ' <button class="reply_comment_btn" data-id="'+cmt_id+'">Gửi</button>';
      }else {
         html_rep += '<input type="text" name="reply_comment_email" class="reply_comment_email" placeholder="Nhập email của bạn (*)" />';                 
         html_rep += '<input type="text" name="reply_comment_name" class="reply_comment_name" placeholder="Nhập tên của bạn (*)" />';              
         html_rep += ' <button class="reply_comment_btn" data-id="'+cmt_id+'">Gửi</button>';
      }               
      html_rep += '</div></div></div>';   
      $(this).closest('.comment_item').after(html_rep);
   });
   $('.comments').on('click','.reply_comment_btn',function(){
      var curr_rep = $(this);
      var parent_rep =curr_rep.closest('.reply_box');
      var rep_comment_id = curr_rep.attr('data-id');
      var rep_email = parent_rep.find('.reply_comment_email').val();
      var rep_name = parent_rep.find('.reply_comment_name').val();
      var rep_content = parent_rep.find('.reply_comment_content').val();
      if(rep_content == '') {
         alert('Vui lòng nhập nội dung bình luận !');
      }else if(rep_email == '') {
         alert('Vui lòng nhập email của bạn !');
      }else if(!isemail(rep_email)) {
         alert('Vui lòng nhập 1 email hợp lệ !');
      }else if(rep_name == '') {
         alert('Vui lòng nhập tên của bạn !');
      }else {
         var data = {name:'addreply',rep_comment_id:rep_comment_id,rep_email:rep_email,rep_name:rep_name,rep_content:rep_content};
         loadAjax(data,'json',{
            success:function(res) {
               if(res.status == 1) {
                  parent_rep.after(res.html);
                  $('.imgloading').remove();
                  $('.reply_comment_content').val('');
                }else {
                  alert(res.html);
                  $('.imgloading').remove();
                  $('.reply_comment_content').val('');                  
               }
            },
            beforeSend:function() {
               $('.reply_comment_meta').append('<img style="display: inherit; margin: 5px auto;" class="imgloading" src="/themes/img/loadingcmt.GIF" />');
            } 
         });
      }
   });
   $('.comments').on('click','.cmt_like',function(){
      var curlike = $(this);
      var like_id = curlike.attr('data-id');
      var like_type = curlike.attr('data-type');
      var like_count = curlike.attr('data-like');
      var curcount = parseInt(like_count) + 1;
      $.ajax({
        type:'get',
        url:'/get-ajax/like',
        data:{style:'like',like_id:like_id,like_type:like_type},
        dataType:'json',
        beforeSend:function(){
            curlike.replaceWith('<span class="cmt_dislike" data-id="'+like_id+'" data-type="'+like_type+'" data-like="'+curcount+'">Không thích <i class="icon_like"></i> <span>'+curcount+'</span></span>');
        },
        success:function(result){
            
        }
    })
   });
   $('.comments').on('click','.cmt_dislike',function(){
      var curlike = $(this);
      var like_id = curlike.attr('data-id');
      var like_type = curlike.attr('data-type');
      var like_count = curlike.attr('data-like');
      var curcount = parseInt(like_count) - 1;
      $.ajax({
        type:'get',
        url:'/get-ajax/like',
        data:{style:'dislike',like_id:like_id,like_type:like_type},
        dataType:'json',
        beforeSend:function(){
            curlike.replaceWith('<span class="cmt_like" data-id="'+like_id+'" data-type="'+like_type+'" data-like="'+curcount+'">Thích <i class="icon_like"></i> <span>'+curcount+'</span></span>');
        },
        success:function(result){
            
        }
    })
   });
   $('.comments').on('click','.more_cmt',function(){
      var morebtn = $(this);
      var more_id = morebtn.attr('data-pid');
      var more_type = morebtn.attr('data-type');
      var more_page = morebtn.attr('data-page');
      more_page = parseInt(more_page);
      var data = {name:'more_comment',more_id:more_id,more_type:more_type,more_page:more_page};
      loadAjax(data,'json',{
         success:function(res) {
            morebtn.find('span').html('');  
            if(res.status == 1) {
               morebtn.before(res.html);
               more_page++;
               morebtn.attr('data-page',more_page);
             }else if(res.status == 2) {
               morebtn.before(res.html);
               morebtn.remove();
             }else {
               morebtn.remove();             
            }
         },
         beforeSend:function() {
            morebtn.find('span').html('<img class="imgloading" src="/themes/img/loadingcmt.GIF" />');
         } 
      });
   });
});